import mx.transitions.Tween;
import mx.transitions.easing.*;
/**
	@author Matthew Richmond
	@version 2005-10-14
*/

class timber.transitions.SkewClip {

	private var obj:MovieClip;
	private var initx:Number;
	private var inity:Number;
	private var outerClip:MovieClip;
	private var innerClip:MovieClip;

	// ===========================================================
	// - CONSTRUCTOR
	// ===========================================================

	function SkewClip (obj:MovieClip, idName:String, outerClipName:String, innerClipName:String, x:Number, y:Number){
		//trace("SkewClip");
		
		// store any needed vars
		this.obj = obj;
		this.initx = x;
		this.inity = y;
		
		// make an outside clip
		outerClip = obj.createEmptyMovieClip(outerClipName, obj.getNextHighestDepth());
		outerClip._x = initx;
		outerClip._y = inity;
		
		// make the inside clip
		innerClip = outerClip.attachMovie(idName, innerClipName, outerClip.getNextHighestDepth());
	};

	// ===========================================================
	// - BASIC SKEW
	// ===========================================================

	function crossDatum(current:Number, desired:Number, datum:Number ):Boolean{
		if ( ((current < datum) && (desired > datum)) || ((current > datum) && (desired < datum)) ) {
			return true;
		} else {
			return false;
		};
	};

	function skew (targx:Number, targy:Number, innerScale:Number, outerScale:Number, rotationDegrees:Number, callback:Object) {
			
			var speed = 1;
			var halfspeed = speed*.5;
			var ease1 = None.easeOut;
			var ease2 = Regular.easeOut;
			
			if (crossDatum (outerClip._x, targx, initx)) {
			
				var tr = new Tween(innerClip, "_rotation", ease1, innerClip._rotation, 0, halfspeed, true);	
				tr.self = this;
				tr.onMotionFinished = function() {
					var clip = this.self.innerClip;
					new Tween(clip, "_rotation", ease2, clip._rotation, rotationDegrees, halfspeed, true);
				};
				
				var tys = new Tween(innerClip, "_yscale", ease1, innerClip._yscale, 100, halfspeed, true);
				tys.self = this;
				tys.onMotionFinished = function() {
					var clip = this.self.innerClip;
					new Tween(clip, "_yscale", ease2, clip._yscale, innerScale, halfspeed, true);
				};
				
				var txs = new Tween(outerClip, "_xscale", ease1, outerClip._xscale, 100, halfspeed, true);
				txs.self = this;
				txs.onMotionFinished = function() {
					var clip = this.self.outerClip;
					new Tween(clip, "_xscale", ease2, clip._xscale, outerScale, halfspeed, true);
				};
				
				var halfx = outerClip._x + ((targx - outerClip._x)*.5);
				var tx = new Tween(outerClip, "_x", ease1, outerClip._x, halfx, halfspeed, true);
				tx.self = this;
				tx.onMotionFinished = function() {
					var clip = this.self.outerClip;
					new Tween(clip, "_x", ease2, clip._x, targx, halfspeed, true);
				};
				
				var halfy = outerClip._y + ((targy - outerClip._y)*.5);
				var ty = new Tween(outerClip, "_y", ease1, outerClip._y, halfy, halfspeed, true);
				ty.self = this;
				ty.onMotionFinished = function() {
					var clip = this.self.outerClip;
					new Tween(clip, "_y", ease2, clip._y, targy, halfspeed, true);
				};
				
			} else {
				
				new Tween(innerClip, "_rotation", Regular.easeOut, innerClip._rotation, rotationDegrees, speed, true);
				new Tween(innerClip, "_yscale", Regular.easeOut, innerClip._yscale, innerScale, speed, true);
				new Tween(outerClip, "_xscale", Regular.easeOut, outerClip._xscale, outerScale, speed, true);
			
				new Tween(outerClip, "_x", Regular.easeOut, outerClip._x, targx, speed, true);
				new Tween(outerClip, "_y", Regular.easeOut, outerClip._y, targy, speed, true);
				
			};
	};

	// ===========================================================
	// - SKEW FROM CENTER
	// ===========================================================

	function skewFromCenter(){
		
	};
};